package com.example.apigateway.controller;

import com.example.apigateway.common.ResultMsg;
import com.example.apigateway.common.UserContext;
import com.example.apigateway.model.User;
import com.example.apigateway.service.AccountService;
import com.example.apigateway.service.AgencyService;
import com.netflix.discovery.converters.Auto;
import netscape.security.UserDialogHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sun.awt.SunHints;
import sun.misc.Request;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-06-23
 **/
@Controller
public class UserController {



    @Autowired
    private AccountService accountService;


    @Autowired
    private AgencyService agencyService;

    //----------------------------注册流程-------------------------------------------
    @RequestMapping(value = "/accounts/register",method = {RequestMethod.POST, RequestMethod.GET})
    public String accountSubmit(User account, ModelMap modelMap) {
        if(account == null || account.getName() == null) {
            modelMap.put("agencyList",agencyService.getAllAgency());
            return "/user/accounts/register";
        }
        ResultMsg retMsg = UserHelper.validate(account);

        if(retMsg.isSuccess()) {
            boolean exist = accountService.isExist(account.getEmail());
            if (!exist) {
                accountService.addAccount(account);
                modelMap.put("success_email",account.getEmail());
                return "/user/accounts/registerSubmit";
            } else {
                return "redirect:/accounts/register?"+ ResultMsg.errorMsg("邮箱已被占用").asUrlParams();
            }

        } else {
            return "redirect:/accounts/register?"+ ResultMsg.errorMsg("参数错误").asUrlParams();
        }
    }

    @RequestMapping("/accounts/verify")
    public String verify(String key) {
        boolean result = accountService.enable(key);
        if (result) {
            return "redirect:/index?" + ResultMsg.successMsg("激活成功").asUrlParams();
        } else {
            return "redirect:/accounts/signin?" + ResultMsg.errorMsg("激活失败,请确认链接是否过期").asUrlParams();
        }

    }


    //----------------------------登录流程-------------------------------------------



    @RequestMapping(value = "/accounts/signin",method = {RequestMethod.POST,RequestMethod.GET})
    public String loginSumbit(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username == null || password == null) {
            request.setAttribute("target", request.getParameter("target"));
            return "/user/accounts/signin";
        }
        User user = accountService.auth(username,password);
        if (user == null) {
            return "redirect:/accounts/signin?" + "username" +username + "&"+ ResultMsg.errorMsg("用户名或密码错误").asUrlParams();
        } else {
            UserContext.setUser(user);
            return StringUtils.isNotBlank(request.getParameter("target"))? "redirect:" + request.getParameter("target"):"redirect:/index";
        }
    }


    @RequestMapping("/accounts/logout")
    public String logout(HttpServletRequest request) {
        User user = UserContext.getUser();
        accountService.logout(user.getToken());
        return "redirect:/index";
    }
}



